﻿//for examples of use in an actual game see Nat Geo fight science loader
import org.casaframework.util.ValidationUtil;
import mx.utils.Delegate;
import arkadium.XMLFile;
import arkadium.EventManager;

import FightScienceLoader_Code.DataManager;
import FightScienceLoader_Code.Main

class FightScienceLoader_Code.SendToFriendPanel extends arkadium.GUIPanel
{
	var amountToMoveDown:Number = 0;
	var firstError:Boolean = false;
	var secondError:Boolean = false;
	var sendToAFriendForm:MovieClip;
	var _sbtSend:Button
	var _sbtSendAnother:Button
	var _sbtReturn:Button
	var thankYou:MovieClip;
	var sendToAFriendXML:XMLFile;
	
	var _smcTopBar:MovieClip
	
	
	private function SendToFriendPanel(){
		super.registerPanel( this, "SENDTOFRIEND_PANEL" );
	}
	
	public function onInitialize(){
		gotoAndStop("startFrame");
		super.onInitialize()
		super.showMe( false );
		//init();
		
	}
	
	function sendBtnPress(){
		Main.getInstance()._cSoundManager.clickSoundStart();
		validateForm();
	}
	
	function sendAnotherBtnPress(){
		Main.getInstance()._cSoundManager.clickSoundStart();
		init();
	}
	
	function returnBtnPress(){
		Main.getInstance()._cSoundManager.clickSoundStart();
		init();
		super.showMe(false);
	}
	
	function validateForm(){
		trace("validateForm");
		// clear old errors
		removeErrorMessages();
		
		// trace(ValidationUtil.isEmail(yourEmail.text));
		var formIsValid:Boolean = true;
		
		if(!ValidationUtil.isEmail(sendToAFriendForm.yourEmail.text)){ 
			formIsValid = false;
			if(!firstError){
				addFirstError();
			}
			if(sendToAFriendForm.yourEmail.text == ""){
				sendToAFriendForm.yourEmail.text = "Please type your e-mail address.";
			} else {	
				sendToAFriendForm.yourEmail.text = "Please use this format:name@example.com";
			}
		} else {
			if(firstError){
				removeFirstError();
			}
		}

		if(!ValidationUtil.isEmail(sendToAFriendForm.friendsEmail.text)) {
			formIsValid = false;
			if(!secondError){
				addSecondError();
			}
			if(sendToAFriendForm.friendsEmail.text == ""){
				sendToAFriendForm.friendsEmail.text = "Please type your e-mail address.";
			} else {	
				sendToAFriendForm.friendsEmail.text = "Please use this format:name@example.com";
			}
		} else {
			if(secondError){
				removeSecondError();
			}
		}
		
		if(formIsValid){
			sendForm();
		} else {
			fixErrors();
		}
		
		_smcTopBar.onEnterFrame = function(){
			var my_date:Date = new Date();
			var hours = my_date.getHours();
			hours = hours%12;
			if (hours == 0)hours = 12;
			var timeString = hours
			
			var minutes = my_date.getMinutes()
			if (minutes < 10)minutes = "0"+minutes;
			timeString += ":"+minutes+" ";
			timeString += (my_date.getHours()<12) ? "AM" : "PM";
			this._sdtTime.text = timeString;
		}
	}
	
	function sendForm(){
		trace("Form is valid");
		loadData();
	}
	
	function fixErrors(){
		trace("Errors have been found.");
	}
	
	function removeErrorMessages(){
		sendToAFriendForm.yourEmailError.text = "";
		sendToAFriendForm.friendsEmailError.text = "";
		sendToAFriendForm.messageError.text = "";
	}
	
	function init(){
		gotoAndStop("startFrame");
		
		thankYou._visible = false;
		
		sendToAFriendForm.yourEmailError._visible = false;
		sendToAFriendForm.friendsEmailError._visible = false;
		
		sendToAFriendForm.yourName.text = "Name"
		sendToAFriendForm.yourEmail.text = ""
		sendToAFriendForm.friendsEmail.text = ""
		sendToAFriendForm.subjectText.text = "Subject"
		sendToAFriendForm.messageText.text = "This is a great game.  I dare you to try and beat my score (click the link below)"
		
		_sbtSend.onRelease = function(){_parent.sendBtnPress();}
		_sbtSend.onRollOver  = function(){ Main.getInstance()._cSoundManager.rolloverSoundStart();   }
		_sbtReturn.onRelease = function(){_parent.returnBtnPress();}
		_sbtReturn.onRollOver  = function(){ Main.getInstance()._cSoundManager.rolloverSoundStart();   }
		
		sendToAFriendXML = new XMLFile();
	}
	
	
	
	function loadData(){
		trace("loadData()");
		var sSendToFriendURL = Main.getInstance()._cAppConfigFile.getData("sendtofriendUrl");
		if(sSendToFriendURL==undefined)sSendToFriendURL = "http://72.3.138.97/ngservice/sendtofriend.aspx"//"http://testing.arkadium.com/arkadium_leaderboard/sendtofriend.aspx"
		var sUserName = DataManager.getInstance()._sUserName
		if(sUserName == undefined)sUserName = "AAA";
		var sUnencryptedParams = 'fromName='+sendToAFriendForm.yourName.text+'&fromEmail='+sendToAFriendForm.yourEmail.text+'&toEmail='+sendToAFriendForm.friendsEmail.text+'&subject='+sendToAFriendForm.subjectText.text+'&message='+sendToAFriendForm.messageText.text + ""+DataManager.getInstance().generateNewChallenge();
		trace(sUnencryptedParams);
		var sParams = Main.getInstance()._cEncryptionManager.encryptParams(sUnencryptedParams) 
		//'username='+sUserName+'&score='+nCumulativeScore+'&top=9&showMyRank=1');
		trace("sParams"+sParams );
		gotoAndStop("sendingFrame");
		sendToAFriendXML.loadFile( this, onSendToAFriendXML, sSendToFriendURL+ "?params="+sParams );
	
		
		
		//sendToAFriendLV.message = sendToAFriendForm.radioGroup.selectedData;
		// var urlPrefix:String = (System.capabilities.playerType == "External") ? "http://pt2007.dev.biggs-gilmore.com" : "";
		// treatMakerLV.sendAndLoad(urlPrefix+"/cgi-bin/pt/phase2/pf_profanity_check.pl", treatMakerLV, "POST");
		//sendToAFriendLV.sendAndLoad("http://jtriemstraxp/emailafriend.aspx", sendToAFriendLV, "POST");
	}
	
	function onSendToAFriendXML(bSuccess:Boolean){
		if(bSuccess){
			trace("data received! " + sendToAFriendXML);
		} else {
			trace("error received! " + sendToAFriendXML);
		}
		gotoAndStop("sentFrame");
		
		trace("there are "+DataManager.getInstance()._cMyCharacter._aHistory.length+" saved events");
		DataManager.getInstance()._cMyCharacter._aHistory[DataManager.getInstance()._cMyCharacter._aHistory.length] = {situation:"sent",friend:"unknown"};
		DataManager.getInstance().saveCharacterData();
		
		_sbtSendAnother.onRelease = function(){_parent.sendAnotherBtnPress();}
		_sbtSendAnother.onRollOver  = function(){ Main.getInstance()._cSoundManager.rolloverSoundStart();   }
		_sbtReturn.onRelease = function(){_parent.returnBtnPress();}
		_sbtReturn.onRollOver  = function(){ Main.getInstance()._cSoundManager.rolloverSoundStart();   }
		
	}
	
	function addFirstError(){
		firstError = true;
		sendToAFriendForm.yourEmailError._visible = true;
		
		sendToAFriendForm.disclaimer._y += amountToMoveDown;
		sendToAFriendForm.sendButton._y += amountToMoveDown;
		sendToAFriendForm.text3._y += amountToMoveDown;
		sendToAFriendForm.friendsEmail._y += amountToMoveDown;
		sendToAFriendForm.text2._y += amountToMoveDown;
		sendToAFriendForm.yourEmail._y += amountToMoveDown;
		sendToAFriendForm.friendsEmailError._y += amountToMoveDown;
	}
	
	function removeFirstError(){
		firstError = false;
		sendToAFriendForm.yourEmailError._visible = false;
		
		sendToAFriendForm.disclaimer._y -= amountToMoveDown;
		sendToAFriendForm.sendButton._y -= amountToMoveDown;
		sendToAFriendForm.text3._y -= amountToMoveDown;
		sendToAFriendForm.friendsEmail._y -= amountToMoveDown;
		sendToAFriendForm.text2._y -= amountToMoveDown;
		sendToAFriendForm.yourEmail._y -= amountToMoveDown;
		sendToAFriendForm.friendsEmailError._y -= amountToMoveDown;
	}
	
	function addSecondError(){
		secondError = true;
		sendToAFriendForm.friendsEmailError._visible = true;
		
		sendToAFriendForm.disclaimer._y += amountToMoveDown;
		sendToAFriendForm.sendButton._y += amountToMoveDown;
		sendToAFriendForm.text3._y += amountToMoveDown;
		sendToAFriendForm.friendsEmail._y += amountToMoveDown;
		// sendToAFriendForm.friendsEmailError._y += amountToMoveDown;
	}
	
	function removeSecondError(){
		secondError = false;
		sendToAFriendForm.friendsEmailError._visible = false;
		
		sendToAFriendForm.disclaimer._y -= amountToMoveDown;
		sendToAFriendForm.sendButton._y -= amountToMoveDown;
		sendToAFriendForm.text3._y -= amountToMoveDown;
		sendToAFriendForm.friendsEmail._y -= amountToMoveDown;
		// sendToAFriendForm.friendsEmailError._y -= amountToMoveDown;
	}
}